import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonsParentSwitchChild.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerParent.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/module/SimplifiedReadingBody.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';
import 'package:ichinesereader/widgets/sidebars/parentSidebar.dart';

class ParentLeveledReadings extends StatefulWidget {
  static const String routeName = '/parent/leveledreadings';

  @override
  State<ParentLeveledReadings> createState() => _ParentLeveledReadingsState();
}

class _ParentLeveledReadingsState extends State<ParentLeveledReadings> {
  final box = Hive.box('appconfig');
  final BookSearchDto dto = new BookSearchDto();
  final BooksController _booksController = Get.find();

  @override
  void initState() {
    _booksController.setBookSearchDto(dto);
    Future.delayed(Duration(milliseconds: 100), () {
      String currentchild = box.get(CacheManagerKey['currentchild'].toString());
      _booksController.fetchProgressReadingItemsForChild(currentchild);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build  ParentLeveledReadings page');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.parentPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Leveled Readings'.tr) : null,
      drawer: isSmallScreen ? ParentAppDrawer() : null,
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Container(
              child: Visibility(
                visible: !isSmallScreen,
                child: ParentSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen),
              ),
            ),
            Expanded(
                child: Container(
                    margin: _pageStyles.mainContainerPageEdgeInsets(),
                    child: SimplifiedReadingBody())),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButtonsParentSwitchChild(),
    );
  }
}
